using Gateway.Common.Entity;
using Gateway.Common.Enums;
using Gateway.Common.Extensions;

namespace Gateway.Domain.Entities;

/// <summary>
/// 用户消息
/// </summary>
[PrimaryIdAttribute("UMS")]
public class UserMessage : AggregateRootEntityBase
{
    /// <summary>
    /// 用户名/标识
    /// </summary>
    public string Account { get; set; }

    /// <summary>
    /// 消息
    /// </summary>
    public string Message { get; set; }

    public BooleanStatusEnum ReadStatus { get; set; }

    /// <summary>
    /// 摘要
    /// </summary>
    public string Intro { get; set; }

    /// <summary>
    /// 消息类型
    /// </summary>
    public string SendType { get; set; }

    /// <summary>
    /// 发送人
    /// </summary>
    public string Sender { get; set; }

    /// <summary>
    /// 完成状态
    /// </summary>
    public MessageStateEnum State { get; set; }

    /// <summary>
    /// 结果
    /// </summary>
    public string Result { get; set; }

    /// <summary>
    /// 消息类型
    /// </summary>
    public MessageTypeEnum MessageType { get; set; }


    /// <summary>
    /// 关联的资源id
    /// </summary>
    public string ResourceId { get; set; }

    public UserMessage()
    {
    }

    public UserMessage(string account = default, string message = default, string sendType = default,
        string sender = default, string messageType = default, string state = default, string resourceId = default)
    {
        Account = account;
        Message = message;
        SendType = sendType;
        Intro = ReplaceHtmlHelper.Instance.ReplaceHtmlTag(message ?? "");
        if (Intro.Length > 20)
        {
            Intro = Intro.Substring(0, 20) + "...";
        }

        ReadStatus = BooleanStatusEnum.No;
        Sender = sender;
        MessageType = string.IsNullOrEmpty(messageType) ? MessageTypeEnum.Notify : MessageTypeEnum.Parse(messageType);
        State = string.IsNullOrEmpty(state) ? MessageStateEnum.Success : MessageStateEnum.Parse(state);
        ResourceId = resourceId;
        Result = "";
    }


    /// <summary>
    /// 设置结果
    /// </summary>
    /// <param name="state"></param>
    /// <param name="result"></param>
    /// <param name="message"></param>
    public void SetResult(MessageStateEnum state, string result = default, string message = default)
    {
        Result = result??"";
        State = state;
        if (!string.IsNullOrEmpty(message))
        {
            Message = message;
        }
    }

    /// <summary>
    /// 阅读消息
    /// </summary>
    public void Read()
    {
        ReadStatus = BooleanStatusEnum.Yes;
    }
}